package com.ddpt.permission.export.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.center.common.base.BizEntity;
import com.lh.center.common.export.ExportQueryService;
import com.lh.center.common.export.dto.permission.AdminUserDto;
import com.lh.center.common.export.vo.permission.AdminUserVo;
import com.lh.center.common.util.MarketingUtils;
import com.lh.center.permission.api.entity.BackAdminUser;
import com.lh.center.permission.api.entity.MerchantAdminUser;
import com.lh.center.permission.api.entity.StoreAdminUser;
import com.lh.center.permission.service.BackAdminUserService;
import com.lh.center.permission.service.MerchantAdminUserService;
import com.lh.center.permission.service.StoreAdminUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("adminUserExportService")
public class AdminUserExportServiceImpl implements ExportQueryService<AdminUserDto, AdminUserVo> {
    @Resource
    private BackAdminUserService backAdminUserService;
    @Resource
    private MerchantAdminUserService merchantAdminUserService;
    @Resource
    private StoreAdminUserService storeAdminUserService;

    @Override
    public List<AdminUserVo> query(AdminUserDto dto) {
        ArrayList<AdminUserVo> vos = new ArrayList<>();
        String userType = dto.getUserType();
        switch (userType) {
            case "backAdmin": {
                List<BackAdminUser> list = backAdminUserService.list(Wrappers.lambdaQuery(BackAdminUser.class)
                        .lt(null != dto.getExportEndTime(), BizEntity::getCreateTime, dto.getExportEndTime())
                        .gt(null != dto.getExportStartTime(), BizEntity::getCreateTime, dto.getExportStartTime())
                        .like(null != dto.getUserName(), BackAdminUser::getUsername, dto.getUserName())
                        .eq(null != dto.getStatus(), BackAdminUser::getStatus, dto.getStatus())
                );
                list.stream().forEach(e -> {
                    AdminUserVo vo = new AdminUserVo();
                    BeanUtil.copyProperties(e, vo);
                    vos.add(vo);
                });
            }
            case "merchant": {
                LambdaQueryWrapper<MerchantAdminUser> wrapper = Wrappers.lambdaQuery(MerchantAdminUser.class)
                        .lt(null != dto.getExportEndTime(), BizEntity::getCreateTime, dto.getExportEndTime())
                        .gt(null != dto.getExportStartTime(), BizEntity::getCreateTime, dto.getExportStartTime())
                        .like(null != dto.getUserName(), MerchantAdminUser::getUsername, dto.getUserName())
                        .eq(null != dto.getStatus(), MerchantAdminUser::getStatus, dto.getStatus());
                List<MerchantAdminUser> list = merchantAdminUserService.list(MarketingUtils.buildDataAuthQuery(wrapper, dto.getInsIds(), dto.getUser()));
                list.stream().forEach(e -> {
                    AdminUserVo vo = new AdminUserVo();
                    BeanUtil.copyProperties(e, vo);
                    vos.add(vo);
                });
            }
            case "store": {
                List<StoreAdminUser> list = storeAdminUserService.list(Wrappers.lambdaQuery(StoreAdminUser.class)
                        .lt(null != dto.getExportEndTime(), BizEntity::getCreateTime, dto.getExportEndTime())
                        .gt(null != dto.getExportStartTime(), BizEntity::getCreateTime, dto.getExportStartTime())
                        .like(null != dto.getUserName(), StoreAdminUser::getUserName, dto.getUserName())
                        .eq(null != dto.getStatus(), StoreAdminUser::getStatus, dto.getStatus())
                );
                list.stream().forEach(e -> {
                    AdminUserVo vo = new AdminUserVo();
                    BeanUtil.copyProperties(e, vo);
                    vos.add(vo);
                });
            }
        }

        return vos;
    }
}
